﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace IDReaderDemo
{
    class Identity
    {
        /// <summary>
        /// 初始化成功，1表示成功，其他表示失败
        /// </summary>
        public static int init;

        /// <summary>
        /// 身份证信息
        /// </summary>
        public class Card
        {
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name
            {
                get
                {
                    //IntPtr ptr = Marshal.AllocHGlobal(30);
                    //GetPeopleName(ptr, 30);
                    //string sname = Marshal.PtrToStringAnsi(ptr);
                    //Marshal.FreeHGlobal(ptr);
                    byte[] buf = new byte[30];
                    GetPeopleName(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    return str;
                }
            }

            /// <summary>
            /// 性别
            /// </summary>
            public string Sex
            {
                get
                {
                    byte[] buf = new byte[2];
                    GetPeopleSex(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    string sex = str == "1" ? "男" : str == "0" ? "女" : str;
                    return sex;
                }
            }

            /// <summary>
            /// 民族
            /// </summary>
            public string Nation
            {
                get
                {
                    byte[] buf = new byte[4];
                    GetPeopleNation(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);  
                    return str;
                }
            }

            /// <summary>
            /// 出生日期
            /// </summary>
            public string BirthDay
            {
                get
                {
                    byte[] buf = new byte[16];
                    GetPeopleBirthday(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    return str;
                }
            }

            /// <summary>
            /// 住址
            /// </summary>
            public string Address
            {
                get
                {
                    byte[] buf = new byte[70];
                    GetPeopleAddress(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    return str;
                }
            }

            /// <summary>
            /// 证件号码
            /// </summary>
            public string IDCode
            {
                get
                {
                    byte[] buf = new byte[36];
                    GetPeopleIDCode(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    return str;
                }
            }

            /// <summary>
            /// 签发机关
            /// </summary>
            public string Department
            {
                get
                {
                    byte[] buf= new byte[30];
                    GetDepartment(buf, buf.Length);
                    string str1 = Encoding.Default.GetString(buf);
                    return str1;
                }
            }

            /// <summary>
            /// 有效起始日期
            /// </summary>
            public string StartDate
            {
                get
                {
                    byte[] buf = new byte[16];
                    GetStartDate(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    return str;
                }
            }

            /// <summary>
            /// 有效结束日期
            /// </summary>
            public string EndDate
            {
                get
                {
                    byte[] buf = new byte[16];
                    GetEndDate(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    return str;
                }
            }

            /// <summary>
            /// 保留信息
            /// </summary>
            public string Reserve
            {
                get
                {
                    byte[] buf = new byte[36];
                    GetReserve(buf, buf.Length);
                    string str = Encoding.Default.GetString(buf);
                    return str;
                }
            }

            /// <summary>
            /// 照片信息
            /// </summary>
            public byte[] PhotoBMP
            {
                get
                {
                    byte[] buf = new byte[204800];
                    GetPhotoBMP(buf, buf.Length);
                    //string str = Encoding.Unicode.GetString(buf);
                    return buf;
                }
            }
        }

        /// <summary>
        /// 打开串口并返回串口号，-1表示失败，其他表示成功
        /// </summary>
        /// <returns>串口号</returns>
        public static int OpenAndGetPort()
        {
            int port = -1;
            int[] portArr = new int[] { 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014 };
            for (int i = 0; i < portArr.Length && init != 1; i++)
            {
                init = InitComm(portArr[i]);
                port = init == 1 ? portArr[i] : port;
            }
            return port;
        }

        /// <summary>
        /// 关闭串口，1表示成功，其他表示失败
        /// </summary>
        /// <returns>int</returns>
        public static int Close()
        {
            int close = CloseComm();
            init = close == 1 ? 0 : 1;
            return close;
        }

        /// <summary>
        /// 读取身份证信息，1表示成功，其他表示失败
        /// </summary>
        /// <returns></returns>
        public static int ReadCard()
        {
            //OpenAndGetPort();
            //Authenticate();
            int read = Read_Content(1);
            if (read != 1)
            {
                //若读取不成功，重新认证一遍再读取
                int auth = Authenticate();
                if (auth == 1)
                {
                    read = Read_Content(1);
                }
            }
            //Close();
            return read;
        }


        /// <summary>
        /// 初始化串口
        /// </summary>
        /// <param name="Port"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int InitComm(int Port);

        /// <summary>
        /// 关闭串口
        /// </summary>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int CloseComm();

        /// <summary>
        /// 卡认证
        /// </summary>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int Authenticate();

        /// <summary>
        /// 读卡操作
        /// </summary>
        /// <param name="active"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int Read_Content(int active);

        /// <summary>
        /// 读姓名
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetPeopleName(byte[] strTmp, int strLen);

        /// <summary>
        /// 读性别：1:男 0:女
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>         
        [DllImport("termb.dll")]
        private extern static int GetPeopleSex(byte[] strTmp, int strLen);

        /// <summary>
        /// 读民族信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetPeopleNation(byte[] strTmp, int strLen);

        /// <summary>
        /// 读出生日期
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetPeopleBirthday(byte[] strTmp, int strLen);

        /// <summary>
        /// 读住址
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetPeopleAddress(byte[] strTmp, int strLen);

        /// <summary>
        /// 读身份证号
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetPeopleIDCode(byte[] strTmp, int strLen);

        /// <summary>
        /// 读签发机关
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetDepartment(byte[] strTmp, int strLen);

        /// <summary>
        /// 读有效起始日期
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetStartDate(byte[] strTmp, int strLen);

        /// <summary>
        /// 读有效结束日期
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetEndDate(byte[] strTmp, int strLen);

        /// <summary>
        /// 读保留信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetReserve(byte[] strTmp, int strLen);

        /// <summary>
        /// 读照片信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        private extern static int GetPhotoBMP(byte[] strTmp, int strLen);

    }
}
